package me.zhengjie.modules.rest.enterprise;

import cn.hutool.core.date.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.domain.ApiBatch;
import me.zhengjie.modules.domain.Batch;
import me.zhengjie.modules.domain.enterprise.EnterpriseAbstract;
import me.zhengjie.modules.domain.enterprise.EnterpriseIndustry;
import me.zhengjie.modules.domain.enterprise.EnterpriseInformation;
import me.zhengjie.modules.domain.enterprise.EnterpriseTax;
import me.zhengjie.modules.repository.ApiBatchMapper;
import me.zhengjie.modules.repository.BatchMapper;
import me.zhengjie.modules.repository.enterprise.EnterpriseFinMapper;
import me.zhengjie.modules.service.*;
import me.zhengjie.modules.utils.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Api(tags ="企业基本信息API")
@RestController()
@RequestMapping("/enter")
@RequiredArgsConstructor
public class EnterpriseInfController {

    private final EnterpriseInfService enterpriseInfService;
    private final EnterpriseTaxService enterpriseTaxService;
    private final EnterpriseIndService enterpriseIndService;
    private final EnterpriseFinService enterpriseFinService;
    private final EnterpriseAbsService enterpriseAbsService;

    @GetMapping("/queryAll")
    public List<EnterpriseInformation> queryAll(){
        return enterpriseInfService.queryAll();
    }

    @AnonymousAccess
    @ApiOperation("分页查询企业基本信息")
    @GetMapping("/queryEnterpriseInfByPage")
    public Map<String, Object> queryEnterpriseInfByPage(String name,@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        Map<String, Object> map=new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("name",name);
        List<EnterpriseInformation> informationList=enterpriseInfService.queryEnterpriseInfByPage(map);
        Integer total=enterpriseInfService.queryEnterpriseInfByCount();
        map=new HashMap<>();
        map.put("data",informationList);
        map.put("total",total);
        return map;
    }

    @AnonymousAccess
    @ApiOperation("根据名称查询企业基本信息")
    @GetMapping("/queryEnterpriseByName")
    public Map<String, Object> queryEnterpriseByName(String name){
        Map<String, Object> map=new HashMap<>();
        map.put("enterpriseInf", enterpriseInfService.queryEnterpriseByName(name));
        map.put("enterpriseAbs",enterpriseAbsService.queryEnterpriseAbsByName(name));
        return map;
    }

    @AnonymousAccess
    @ApiOperation("根据id查询企业基本信息")
    @GetMapping("/queryEnterpriseById")
    public Map<String, Object> queryEnterpriseById(@RequestParam("id") Integer id){
        Map<String, Object> map=new HashMap<>();
        EnterpriseInformation ent=enterpriseInfService.queryEnterpriseById(id);
        List<EnterpriseTax> tax=enterpriseTaxService.queryEnterpriseTaxById(ent.getCompaniesId());
        ent.setTax(tax);
        EnterpriseAbstract abs=enterpriseAbsService.queryEnterpriseAbsById(ent.getCompaniesId());
        if (abs == null) {
            abs = new EnterpriseAbstract();
            abs.setResult("暂无简介信息");
        }
        map.put("enterpriseAbs",abs);
        map.put("enterpriseInf", ent);
        map.put("milestone",enterpriseInfService.queryEnterpriseInfMilestone(ent.getCompaniesId(),ent.getEstiblishTime()));
        map.put("evaluation",enterpriseInfService.queryEnterpriseInfEvaluation(ent.getCompaniesId()));
        map.put("ranking",enterpriseInfService.queryEnterpriseInfRankingByIndId(ent.getIndustryAll(),ent.getCompaniesId()));
        return map;
    }

    @ApiOperation("条件查询企业")
    @AnonymousAccess
    @GetMapping("/queryEnterpriseInfoForPageByConditions")
    public Map<String, Object> queryEnterpriseInfoForPageByConditions(String standard,String value,String honor,String industry,String tax,@ApiParam("企业名称") String name,@ApiParam("投资论次") String[] round,@ApiParam("开始年")Integer startDate,@ApiParam("结束年")Integer endDate,@ApiParam("地区")String district,@ApiParam("最小人数")Integer startNum,@ApiParam("最大人数")Integer endNum,@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        System.out.println(honor);
        System.out.println(value);
        List<String> indList= StringUtils.strToList(industry);
        List<String> honorList=StringUtils.strToList(honor);
        List<String> valueList=StringUtils.strToList(value);
        List<String> standardList=StringUtils.strToList(standard);
        if(round!=null&&round.length==0){
            round=null;
        }
        Map<String, Object> map=new HashMap<>();
        map.put("name",name);
        map.put("round",round);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("district",district);
        map.put("startNum",startNum);
        map.put("endNum",endNum);
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("tax",tax);
        map.put("industry",indList);
        map.put("honorList",honorList);
        map.put("valueList",valueList);
        map.put("standardList",standardList);
        List<EnterpriseInformation> list=enterpriseInfService.queryEnterpriseInfoForPageByConditions(map);
        int total=enterpriseInfService.queryEnterpriseInfoForPageByConditionsToTotal(map);
        map=new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @ApiOperation("根据企业名称获取专利总数，投资总数")
    @AnonymousAccess
    @GetMapping("/queryNumberInformation")
    public Map<String, Object> queryNumberInformation(@ApiParam("企业名称")String name){
        return enterpriseInfService.queryNumberInformation(name);
    }

    @ApiOperation("查询企业行业top10")
    @AnonymousAccess
    @GetMapping("/queryEnterpriseInfIndustryTopTen")
    public List<Map<String,Object>> queryEnterpriseInfIndustryTopTen(){
        return enterpriseInfService.queryEnterpriseInfIndustryTopTen();
    }

    @ApiOperation("查询所有行业,企业规模，融资论次，成立日期，所属区域")
    @AnonymousAccess
    @GetMapping("/queryEnterpriseTitle")
    public Map<String, Object> queryEnterpriseTitle(){
        Map<String, Object> map=new HashMap<>();
        List<EnterpriseIndustry> indMenu=enterpriseIndService.queryAllMenuParentIsNull();
        List<String> district=enterpriseInfService.queryEnterpriseInfForDistrict();
        List<String> round=enterpriseFinService.queryRound();
        map.put("indMenu",indMenu);
        map.put("district",district);
        map.put("round",round);
        return map;
    }

}
